//
//  NewsCell.swift
//  MVVM-Swift
//
//  Created by 刘伟 on 16/3/12.
//  Copyright © 2016年 lawrence_liu. All rights reserved.
//

import UIKit

//定义组合类型
typealias NewsPresentable = protocol<ThumbnailPresentable, TitlePresentable>

class NewsCell: UITableViewCell {
    
    //MARK: 构造
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    //MARK: 准备UI
    private func prepareUI(){
        self.backgroundColor = UIColor.blackColor()
        contentView.backgroundColor = UIColor.blackColor()
        //MARK: 添加UI
        contentView.addSubview(postContainer)
        postContainer.addSubview(postImg)
        postContainer.addSubview(postContent)
        
        //MARK: 布局UI
        postContainer.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.left.equalTo(contentView).offset(5)
            make.right.equalTo(contentView).offset(-5)
            make.bottom.equalTo(contentView).offset(-2)
        }
        
        postImg.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(postContainer).offset(0)
            //make.height.equalTo(190)//建议使用比例
            make.height.equalTo((SCREEN_WIDTH - 10) * 4 / 5)
        }
        
        postContent.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(postImg.snp_bottom).offset(5)
            make.left.equalTo(postContainer).offset(8)
            make.right.equalTo(postContainer).offset(-8)
            make.bottom.equalTo(postContainer).offset(-5)
        }
        
    }
    
    //MARK: 懒加载必要组件
    
    //MARK: Cell子容器
    lazy var postContainer: UIView = {
        let object = UIView()
        object.clipsToBounds = true
        object.layer.cornerRadius = 5
        object.backgroundColor = UIColor.whiteColor()
        return object
    }()
    
    //MARK: 推送的大图
    lazy var postImg: UIImageView = {
        let object = UIImageView()
        object.clipsToBounds = true
        object.contentMode = .ScaleAspectFill
        object.backgroundColor = UIColor.orangeColor()
        return object
    }()
    
    //MARK: 推送内容
    lazy var postContent: UILabel = {
        let object = UILabel()
        object.numberOfLines = 0
        object.font = UIFont.boldSystemFontOfSize(14)
        return object
    }()
    
    
    
    
    //ViewModel更新视图内容信息
    func updateWithPresenter(presenter: NewsPresentable){
        presenter.updateTitleLabel(self.postContent)
        presenter.updateImageView(self.postImg)
    }


}
